<style scoped>
.rankingList{
  position: absolute;
  left: 18%;
  top: 0%;
  height: 100%;
  width: 59%;
  padding: 10px;
  overflow: auto;
}
.clock{
    position: absolute;
    left: 1%;
    top: 45%;
    width: 180px;
    height: 12.41%;
    border-radius: 4%;
    background-color: #409eff;
    text-align: center;
    overflow:hidden;
    font-size: 30px;
    color: aliceblue;
    cursor: pointer;
}
.clock:hover{
  background-color: #1781eb;
}
.time{
  position: absolute;
  top: 70%;
  left: 1%;
  font-size:23px;
}

.studyBase{
    width: 100%;
    height: 100%;
    position: relative;
}
.el-dropdown{
    position: absolute;
    left: 1.28%;
    top: 1.28%;
}
.switch{
    position: absolute;
    left: 1.28%;
    top: 90%;
}
.joinGroup{
    position: absolute;
    left: 1.28%;
    top: 9.41%;
}
.searchBase{
  width: 44.87%;
  height: 32.05%;
  text-align: center;
  
}
.searchInpute{
  width: 660px;
  height: 50px;
  margin: 0 auto;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
}

.el-input{
  height: 40px;
  width: 300px;
  margin: 5px auto;
}
.searchResult{
  width: 660px;
  height: 420px;
  margin: 20px auto;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
}
.results{
  display: flex;
  flex-wrap:wrap;
  align-content:flex-start;
  width: 660px;
  height: 420px;
}
.result{
  width: 300px;
  height: 50px;
  margin-top: 20px;
  margin-left: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
}
.result i{
  font-size: 30px;
  margin-top: 10px;
  margin-left: 10px;
  float: left;
}
.result span{
  float: left;
  width: 145px;
  font-size: 20px;
  margin-top: 10px;
  margin-left: 10px;
  overflow:hidden;
  white-space:nowrap;
  text-overflow: ellipsis;
}
.result button{
  float: right;
  font-size: 18px;
  height: 50px;
}
.clockData{
    width: 100px;
    height: 50px;
    background-color: #409eff;
    color: azure;
    cursor: pointer;
    border-radius: 30px;
    text-align: center;
    line-height: 50px;
}
.clockData:hover{
  width: 120px;
  transition: all 0.5s;
}
.clockRecord{
  position: relative;
  float: right;
  width: 19%;
  height: 95.5%;
  padding: 20px;
  overflow: auto;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
}
.clockRecordTitle{
  width: 100%;
  height: 30px;
  text-align: center;
  line-height: 30px;
}
.myEcharts{
  width: 700px;
  height: 500px;
}
.echartTitle{
  width: 100%;
  height: 30px;
  text-align: center;
  line-height: 30px;
  font-size: 20px;
}
.before,.after{
  height: 100%;
  width: 80px;
  background-color: #409eff;
  color: aliceblue;
  border: none;
  border-radius: 15px;
  font-size: 25px;
}
.before:hover,.after:hover{
  background-color: #1781eb;
  cursor: pointer;
}
.progress{
  position: absolute;
  top: 80%;
  left: 1%;
  width: 15%;
}
.pages{
 position: absolute;
 bottom: 0;
}
.messageBody{
  position: relative;
  height: 800px;
  width: 290px;
}
.msContent{
  font-size:30px;
  font-family: 'STKaiti';
  display: block;
  width: 100%;
  height: 100%;
  overflow: auto;
}
.msContentX{
  font-size:30px;
  font-family: 'STKaiti';
  display: block;
  width: 100%;
  word-break:break-all
}
@media screen and (max-width: 1600px){

}
</style>

<template>
  <div style="width:100%;height:100%">
    <div class="studyBase" v-show="!isGrpEmpty">
    <div class="rankingList" ref="rankingList">

    </div>
    <el-dropdown @command="grpChange">
      <el-button type="primary" >
        我的群组<i class="el-icon-arrow-down el-icon--right"></i>
      </el-button>
      <el-dropdown-menu slot="dropdown" >
        <el-dropdown-item v-for="(p,index) in userGrps" :key="index" :command="p">
          {{p}}
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <!-- 加入群组模块 -->
    <el-popover
    placement="right"
    class="joinGroup"
    trigger="click"
    >
      <div class="searchBase">
        <div class="searchInpute">
          <el-input
          placeholder="请输入群组名(不输入则显示所有群组)"
          prefix-icon="el-icon-search"
          v-model="grpname"
          >
          </el-input>
          <el-button type="primary" icon="el-icon-search" circle @click="search"></el-button>
        </div>
        <div class="searchResult" v-loading="resultLoading">
          <!-- 空状态 -->
          <el-empty :image-size="200" v-show="isEmpty"></el-empty>
          <!-- 搜索结果 -->
          <div class="results">
            <div class="result" v-show="!isEmpty" v-for="(rs,index) in searchResults" :key="index">
              <i class="el-icon-s-home"></i>
              <span :title="rs">{{rs}}</span>
              <el-button type="primary" icon="el-icon-circle-plus-outline" @click="joinGrp(rs)">加入</el-button>
            </div>
          </div>
        </div>
      </div>
      <el-button type="primary" slot="reference">
        加入群组<i class="el-icon-circle-plus-outline"></i>
      </el-button>
    </el-popover>

    <!-- 查看个人数据模块 -->
    <el-popover
    class="switch"
    placement="top-start"
    trigger="hover"
    >
      <div class="myEcharts" ref="echarts">

      </div>
      <div class="echartTitle">
        本周每日打卡时长
      </div>
      <div class="clockData" slot="reference">数据统计</div>
    </el-popover>
    <p class="time">{{time}}</p>
    <el-button type="primary" :loading="clockLoading" class="clock" @click="bgClock">{{clockStatus}}</el-button>
    <div class="progress" :title="'目标时间：'+goalTime+'小时'">
      <p>本周打卡进度：</p>
      <el-progress :text-inside="true" :stroke-width="26" :percentage="clockLoadTime" ></el-progress>
    </div>
    <!-- 群组消息 -->
    <div class="clockRecord">
      <div class="clockRecordTitle">
        群组消息
      </div>
      <el-empty :image-size="200" description="暂无消息" v-show="isMessmageEmpty" style="margin-top:60%"></el-empty>
      <div class="messageBody" v-show="!isMessmageEmpty">
        <el-timeline  >
          <el-timeline-item v-for="(p,index) in grpMessage" :key="index" :timestamp="p.releasetime" placement="top">
            <el-card>
            <h4>{{p.nickname}}</h4>
<!--            <h4>{{p}}</h4>-->
            <p>发布于 {{p.releasetime}}</p>
            <el-button @click="showMessage(p)" type="primary" size="mini">
              查看消息
            </el-button>
            </el-card>
          </el-timeline-item>
        </el-timeline>
        <el-drawer
          :title="message.groupname"
          :visible.sync="drawer"
          >
          <span class="msContent">
            <p class="msContentX" style="font-size:30px;font-family: 'STKaiti';" v-for="(p,index) in message.content" :key="index" v-html="p">{{p}}<br></p>
          </span>
        </el-drawer>
        <div class="pages">
          <el-pagination
        style="width:100%"
        layout="prev, pager, next"
        :pager-count="5"
        :size="5"
        :page-size="5"
        :total="allMessage"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        >
          </el-pagination>
        </div>
      </div>
      
    </div>
    
    </div>
    <div class="studyBase" style="overflow: auto;" v-show="isGrpEmpty">
      <el-empty :image-size="350" description="暂无群组">
        <el-popover
        placement="top"
        trigger="click"
        >
          <div class="searchBase">
            <div class="searchInpute">
              <el-input
              placeholder="请输入群组名(不输入则显示所有群组)"
              prefix-icon="el-icon-search"
              v-model="grpname"
              >
              </el-input>
              <el-button type="primary" icon="el-icon-search" circle @click="search"></el-button>
            </div>
            <div class="searchResult" v-loading="resultLoading">
              <!-- 空状态 -->
              <el-empty :image-size="200" v-show="isEmpty"></el-empty>
              <!-- 搜索结果 -->
              <div class="results">
                <div class="result" v-show="!isEmpty" v-for="(rs,index) in searchResults" :key="index">
                  <i class="el-icon-s-home"></i>
                  <span :title="rs">{{rs}}</span>
                  <el-button type="primary" icon="el-icon-circle-plus-outline" @click="joinGrp(rs)">加入</el-button>
                </div>
              </div>
            </div>
          </div>
          <el-button type="primary" slot="reference">
            加入群组<i class="el-icon-circle-plus-outline"></i>
          </el-button>
        </el-popover>
      </el-empty>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import dayjs from 'dayjs'

export default {
    name:'study',
    data() {
      return {
        status:0,
        message:{},
        isGrpEmpty:true,
        username:"",
        clockLoading:false,
        drawer:false,
        time:'',
        timerDel:{},
        // 用户输入的搜索内容
        grpname:'',
        // 搜索结果
        searchResults:[],
        // 显示搜索结果是否为空
        isEmpty:true,
        // 群组消息是否为空
        isMessmageEmpty:true,
        // 搜索加载图标的显示控制
        resultLoading:false,
        // 群组打卡信息
        grpAllTime:{},
        // 本人打卡数据
        ownTime:[],
        // 本人打卡总时间
        ownAllTime:0,
        // 群组打卡数据
        grpTime:[[],[]],
        // 群组消息
        grpMessage:[],
        // 用户加入的所有群组
        userGrps:[],
        // 用户当前群组
        nowGrp:"",
        currentPage:1,
        allMessage:0,
        goalTime:1
      }
    },
    computed:{
      clockLoadTime(){
        if(this.goalTime <= this.ownAllTime){
          return 100;
        }else{
          return (this.ownAllTime/this.goalTime*100).toFixed(1);
        }
      },
      clockStatus(){
        if(this.status==0){
          return "上班打卡"
        }else{
          return "下班打卡"
        }
      }
    },
    methods: {
      // 开始打卡
      bgClock(){
        this.clockLoading = true;
        this.axios.post('/click',{},{params:{groupname:this.nowGrp}}).then(res=>{
          if(res.data.code==200){
            this.clockLoading = false;
            this.$message.success(res.data.msg);
            this.getTime();
            if(this.status == 0){
              this.status = 1
            }else{
              this.status = 0;
            }
          }else{
            this.clockLoading = false;
            this.$message.warning(res.data.msg);
          }
        }).catch(err=>{
          console.log(err);

        })
      },
      // 获取用户所加入的所有群组名称
      getUserGrps(){
        this.axios.post('/group/selectGroup',{},{}).then(res=>{
          console.log(res.data);
          this.userGrps = res.data.data;
          this.nowGrp = this.userGrps[0];
          if(this.userGrps.length!=0){
            this.isGrpEmpty = false;
          }
          this.getTime();
        }).catch(err=>{
          console.log(err);
        })
      },
      // 用户改变群组时
      grpChange(command){
        this.nowGrp = command;
        this.getTime();
      },
      // 获取打卡数据
      getTime(){
        this.axios.post('/group/selectGroupalltime',{},{params:{groupname:this.nowGrp}}).then(res=>{
          console.log("123321");
          console.log(res.data)
          this.grpAllTime = res.data;
          this.getOwnTime();
          this.getGrpTime();
          // this.getGrpMessage();
          this.getAllMessages();
        }).catch(err=>{
          console.log(err);
        })
      },
      // 获取个人打卡数据
      getOwnTime(){
        this.axios.post('/user/getUsername',{},{}).then(res=>{
          console.log(res.data)
          this.username = res.data+'';
          for(let i in this.grpAllTime){
            if(this.grpAllTime[i].username == res.data+''){
              let {dayone,daytwo,daythree,dayfour,dayfive,daysix,dayseven} = this.grpAllTime[i];
              this.ownTime = [dayone,daytwo,daythree,dayfour,dayfive,daysix,dayseven];
              this.ownAllTime = this.grpAllTime[i].weektime;
              this.goalTime = this.grpAllTime[i].goaltime;
              this.status = this.grpAllTime[i].status;
              this.myEchart();
              break;
            }
          }
          this.clockLoad();
        }).catch(err=>{
          console.log(err);
        })
      },
      // 获取群组成员打卡信息
      getGrpTime(){
        this.grpTime[0] = [];
        this.grpTime[1] = [];
        for(let i in this.grpAllTime)
        {
          this.grpTime[0].push(this.grpAllTime[i].nickname);
          this.grpTime[1].push(this.grpAllTime[i].weektime);
        }
        this.teamEchart();
      },
      // 获取群消息个数
      getAllMessages(){
        this.axios.post('/group/selectcntanno',{},{params:{groupname:this.nowGrp}}).then(res=>{
          this.allMessage = res.data.data;
          this.getGrpMessage();
          if(res.data.data!=0)
          {
            this.isMessmageEmpty = false;
          }else{
            this.isMessmageEmpty = true;
          }
        }).catch(err=>{
          console.log(err);
        })
      },
      // 获取群消息
      getGrpMessage(){
        this.axios.post('/group/selectAnnounce',{groupname:this.nowGrp,pageanno:this.currentPage},{}).then(res=>{
          console.log(res.data)
          this.grpMessage = [];
          this.grpMessage = res.data;

          this.grpMessage.forEach(item=>{
            this.grpAllTime.forEach(i=>{
                if(item.username===i.username){
                  item.nickname=i.nickname
                }
            })
          })

        }).catch(err=>{
          console.log(err);
        })
      },
      // 构建群组所有成员打卡时间表
      teamEchart(){
        var RankingList = echarts.init(this.$refs.rankingList)   
        RankingList.setOption({
        dataZoom: [
            {
                id: 'dataZoomY',
                type: 'slider',
                yAxisIndex: [0],
                filterMode: 'filter',
                // start: 20,
                // end: 80
            },
            {
                type: 'inside',
                yAxisIndex: 0,
            }
        ],
        title:{
          text:this.nowGrp,
          textStyle: {
          fontSize: 23,
          fontStyle: "normal",
          fontFamily: "Courier New",
          // fontStyle: "italic",
          textBorderColor: "rgba(0, 204, 255, 1)",
          textBorderWidth: 3.5
          },
        },
        tooltip: {
        trigger: 'axis',
        axisPointer: {
        type: 'shadow'
          }
        },
        yAxis: {
          type: 'category',
          name:'成员',
          data: this.grpTime[0],
          axisTick: {
          alignWithLabel: true
          },
          nameTextStyle: {
          fontWeight: "bold",
          fontSize: 15
          },
          axisLabel: {
          fontWeight: "bold",
          fontSize: 18,
          overflow: "truncate",
          ellipisi:'...',
          width:85
          }
        },
        xAxis: {
          name:'小时',
          nameTextStyle: {
          fontWeight: "bold",
          fontSize: 15
          }
        },
        series: [
          {
            name: '时间',
            type: 'bar',
            data: this.grpTime[1],
            showBackground: true,
            backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)'
            },            
            universalTransition: {
            enabled: true,
            divideShape: 'clone'
            }
          }
        ]
      })
      window.addEventListener('resize', function () {
          RankingList.resize();
      });
      },
      //构建个人打卡时间统计表
      myEchart(){
        var myChart = echarts.init(this.$refs.echarts)
        myChart.setOption({
        tooltip: {
        trigger: 'axis',
        axisPointer: {
        type: 'shadow'
          }
        },
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisTick: {
          alignWithLabel: true
          }
        },
        yAxis: {
          name:'打卡时间/小时',
          max:24
        },
        series: [
          {
            name: '时间',
            type: 'bar',
            data: this.ownTime,
            showBackground: true,
            backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)'
            },
            universalTransition: {
            enabled: true,
            divideShape: 'clone'
            }
          }
        ]
      })
      },
      // 首页时间显示
      timer(){
        this.axios.post('/display/time',{},{}).then(res=>{
        console.log("time");
        console.log(res.data)
        this.time = res.data.msg.slice(0,16)
        }).catch(err=>{
        console.log(err);
        })
      },
      // 搜索
      search(){
        this.isEmpty = false;
        this.resultLoading = true;
        this.axios.post('/group/selectGroupname',{},{params:{value:this.grpname}}).then(res=>{
          console.log(res.data)
          this.searchResults = res.data;
          if(this.searchResults.length == 0)
          {
            this.resultLoading = false;
            this.isEmpty = true;
          }else{
            this.resultLoading = false;
          }
        }).catch(err=>{
          console.log(err);
        })
      },
      // 加入群组
      joinGrp(rs){
        this.axios.post('/group/join',{},{params:{groupname:rs}}).then(res=>{
          console.log("加入群组返回值")
          console.log(res)
          if(res.data){
            this.$message.success(res.data.msg)
            window.setTimeout(()=>{
              location.reload();
            },1000)

          }else{
            this.$message.warning("你已在群中")
          }
        }).catch(err=>{
          console.log(err);
        })
      },
      // 用户改变页数
      handleCurrentChange(val)
      {
        this.currentPage = val;
      },
      showMessage(p){
        this.drawer = true;
        this.message = p;
        let str = this.message.content.split("\n")
        this.message.content = str;
      }

    },
    watch: {
      currentPage:{
        immediate:true,
        handler(newValue,oldValue){
          this.getGrpMessage()
        }
      }
    },
    mounted() {
      this.getUserGrps();
      this.timer()
      this.timerDel = setInterval(()=>{
                        this.timer()
                      },1000*60)
       

    },
    beforeDestroy() {
       echartsInstance.dispose()
       clearInterval(this.timerDel)
    },
}

</script>

